﻿using System;
using System.Collections.Generic;

// 定义形状接口  
public interface IShape
{
    double CalculateArea();
    bool IsValid(); // 判断形状是否合法  
}

// 抽象类 Shape  
public abstract class Shape : IShape
{
    public abstract double CalculateArea();
    public abstract bool IsValid();
}

// 长方形类  
public class Rectangle : Shape
{
    public double Width { get; set; }
    public double Height { get; set; }

    public Rectangle(double width, double height)
    {
        Width = width;
        Height = height;
    }

    public override double CalculateArea()
    {
        return Width * Height;
    }

    public override bool IsValid()
    {
        return Width > 0 && Height > 0;
    }
}

// 正方形类  
public class Square : Shape
{
    public double SideLength { get; set; }

    public Square(double sideLength)
    {
        SideLength = sideLength;
    }

    public override double CalculateArea()
    {
        return SideLength * SideLength;
    }

    public override bool IsValid()
    {
        return SideLength > 0;
    }
}

// 圆形类  
public class Circle : Shape
{
    public double Radius { get; set; }

    public Circle(double radius)
    {
        Radius = radius;
    }

    public override double CalculateArea()
    {
        return Math.PI * Radius * Radius;
    }

    public override bool IsValid()
    {
        return Radius > 0;
    }
}

// 主程序  
class Program
{
    static void Main(string[] args)
    {
        Random random = new Random();
        List<Shape> shapes = new List<Shape>();

        // 随机创建 10 个形状对象  
        for (int i = 0; i < 10; i++)
        {
            int shapeType = random.Next(1, 4); // 1 = Rectangle, 2 = Square, 3 = Circle  
            switch (shapeType)
            {
                case 1: // 长方形  
                    shapes.Add(new Rectangle(random.NextDouble() * 10 + 1, random.NextDouble() * 10 + 1));
                    break;
                case 2: // 正方形  
                    shapes.Add(new Square(random.NextDouble() * 10 + 1));
                    break;
                case 3: // 圆形  
                    shapes.Add(new Circle(random.NextDouble() * 10 + 1));
                    break;
            }
        }

        // 计算所有有效形状的面积之和  
        double totalArea = 0;
        foreach (var shape in shapes)
        {
            if (shape.IsValid())
            {
                double area = shape.CalculateArea();
                Console.WriteLine($"形状类型: {shape.GetType().Name}, 面积: {area}");
                totalArea += area;
            }
        }

        Console.WriteLine($"所有有效形状的总面积: {totalArea}");
    }
}